# -*-coding:utf-8 -*-
# @Time: 2023/2/20 0020 下午 13:36
# @Author: Cwoner
# @Organization: CTIM
# @Software: PyCharm

import requests
from lxml import etree
import hashlib
from setting import SAVEPATH
import os
import time
import re

from tools.serverAPI import upload_file
from tools.give_an_alarm import send_email


class TieLingRBao():
    def __init__(self, ccdate, id=''):  # 20230101
        self.url = f'http://szb.yiitai.com/api/tablePageController/getTablePageList?pageDate={ccdate[:4]}-{ccdate[4:6]}-{ccdate[6:]}&accessName=tlrb'
        self.ccdate = ccdate
        self.id = id
        self.name = '铁岭日报'
        print('初始化：', self.name, self.url)
        self.mid = hashlib.md5((self.name + '_baozi').encode()).hexdigest()
        if not os.path.isdir(SAVEPATH + self.mid):
            os.mkdir(SAVEPATH + self.mid)

    def index(self, c=0):
        headers = {
            "Accept": "application/json, text/plain, */*",
            "Accept-Language": "zh,zh-CN;q=0.9",
            "Connection": "keep-alive",
            "Referer": "http://szb.yiitai.com/pc/index.html",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
        }
        try:
            response = requests.get(self.url, headers=headers, verify=False)
        except:
            c += 1
            if c > 2:
                send_email(f'报纸采集系统异常-{self.name}-{int(time.time())}',
                           f'主页请求器异常，异常次数超3次，采集退出。报纸：{self.name}. mid: {self.mid}.  报纸日期：{self.ccdate}.')
                return
            return self.index(c)
        if response.status_code == 200:
            data = response.json()
            bz_list = data['list']
            i = 0
            for banItem in bz_list:
                id = banItem['id']
                title = str(i) + banItem['pageName']
                if not title:
                    title = f'未知标题{i}'
                title = re.sub('\s', '', title)
                pdf_url = f'http://125.39.236.20:8080/tablePageController/downPdf?accessName=tlrb&id={id}'
                print(self.name, title, pdf_url)
                tid = hashlib.md5((self.name + title + self.ccdate).encode()).hexdigest()
                file = tid + '.pdf'
                self.__download(file, pdf_url, tid, title)
                i += 1

    def __download(self, file, url, tid, title):
        headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
        }
        response = requests.get(url, headers=headers, verify=False)
        if response.status_code == 200:
            path = SAVEPATH + self.mid + '/' + self.ccdate + '/'
            if not os.path.isdir(path):
                os.mkdir(path)
            with open(path + file, 'wb', ) as f:
                f.write(response.content)
            data = {
                'entity_id': self.mid,
                'title': title,
                'tid': tid,
                'file_name': file,
                'origin_url': url,
                'ndate': self.ccdate[:4] + '-' + self.ccdate[4:6] + '-' + self.ccdate[6:]
            }
            upload_file(data, response.content)

    def run(self):
        self.index()


if __name__ == '__main__':
    ccdate = '20230905'
    hr = TieLingRBao(ccdate)
    hr.run()
